
var defaultData={

};

var timeFn=null;

columns=[[
    {
        field : 'ck',
        checkbox : true
    },{
        field:'id',
        title:'序号',
        align : 'center',
        width:200,
        hidden : true
    },{
        field:'evpName',
        title:'部门名称',
        align : 'center',
        width:200
    },{
          field:'evpCode',
          title:'部门编码',
          align : 'center',
          width:200
    },{
        field:'evpType',
        title:'部门类型',
        align : 'center',
        width:200
    },{
        field:'parentName',
        title:'父级部门',
        align : 'center',
        width:200,
        formatter: function(value,row,index){
            return '<span title="'+value+'">'+value+'</span>'
        }
    },{
      field:'parentId',
      title:'父级部门Id',
      align : 'center',
      width:200,
      hidden : true
    },{
       field:'orderNum',
       title:'部门排序',
       align : 'center',
       width:100
     }
]];

$(function(){
    // 部门表格
    var deptDataGrid = $('#deptDataGrid').datagrid({
        url:'/eDepart/list',
        method:'get',
        columns: columns,
        rownumbers: true,
        pagePosition: 'bottom',
        pagination: true,
        pageList: [10,20,30,40,50],
        idField: 'id',
        pageSize:10,
        pageNumber: 1,
        striped: true,
        singleSelect:true
    });

    var p = deptDataGrid.datagrid('getPager');

    // 设置部门分页中文
    p.pagination({
        beforePageText: '第',
        afterPageText:'共{pages}页',
        displayMsg: '当前显示 {from} 到 {to}条记录 ,共{total}条记录'
    });

    // 部门树
    $('#deptTree').tree({
        url: '/eDepart/tree',
        method: 'get',
//        checkbox:true,
        loadFilter: function(data){
                if (data.d){
                    return data.d;
                } else {
                    return data;
                }
            },
        onClick: function(node){
            clearTimeout(timeFn);
            timeFn=setTimeout(function(){
                clickTreeNode(this,node);
            },300);
        },
        onDblClick: function(node){
            clearTimeout(timeFn);
            dblClickTreeNode(this,node);
        }

    });

    // 编辑区域窗口
    $('#editDeptWindow').window({
        title : '编辑部门',
        width : 550,
        modal : true,
        shadow : true,
        closed : true,
        height : 400,
        resizable : false
    });

    // 部门名称下拉框
    $('#deptNameCBBox').combobox({
        method:'get',
        valueField:'evpName',
        textField:'evpName',
        hasDownArrow: false,
        panelHeight: 'auto',
        onChange:function(newValue,oldValue){
             if (newValue != null && newValue !== "") {
                  $('#deptNameCBBox').combobox('reload','/eDepart/list/combobox?evpName='+newValue);
             }
             if (newValue ==="") {
                 $('#deptNameCBBox').combobox('loadData', {});
                 $("#deptNameCBBox").combobox('clear').combobox('loadData', {});
               }
        }
    });

    // 部门CODE下拉框
    $('#evpCodeCBBox').combobox({
        method:'get',
        valueField:'evpCode',
        textField:'evpCode',
        hasDownArrow: false,
        panelHeight: 'auto',
        onChange:function(newValue,oldValue){
             if (newValue != null && newValue !== "") {
                  $('#evpCodeCBBox').combobox('reload','/eDepart/list/combobox?evpCode='+newValue);
             }
             if (newValue ==="") {
                 $('#evpCodeCBBox').combobox('loadData', {});
                 $("#evpCodeCBBox").combobox('clear').combobox('loadData', {});
               }
        }
    });
});

function clickTreeNode(ele,node){

}

function dblClickTreeNode(ele,node){

}

/*
    点击部门树上的【添加】按钮
*/
function addDeptNode(){
    // 追加若干个节点并选中他们
    var selected = $('#deptTree').tree('getSelected');
    if(selected!=null){
        var data={
            evpName:'新节点',
            parentId:selected.id
        };

        apiDeptManageAddDept(JSONToStr(data),function(res){
            if(res.status==200){
                $('#deptTree').tree('reload');
                $('#deptDataGrid').datagrid('reload');
            }
        });
    }
}

/*
    打开部门编辑窗口
*/
function editDeptWindowOpen(){
    $('#editDeptWindow').window('open');

    var selected = $('#deptTree').tree('getSelected');

    apiDeptManageGetDept(selected.id,function(res){
        if(res.status==null){
            $("#editDeptForm").form('load',res);
        }else{
            var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/no.png'/><span style='margin-left:10px'>获取对象失败</span></span>"
            messager('失败',tip,4000,'info');
        }
    });
}

/*
    提交编辑部门数据
*/
function submitEditForm(){
    var checkResult=checkValidateForm($('#editDeptForm'));

    if(checkResult){
        var fields = $("#editDeptForm").find("input").serializeArray();
        var dataJsonStr=convertFormDataToJsonStr(fields);

        apiDeptManageEditDept(dataJsonStr,function(res){
            if(res.status==200){
                var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/ok.png'/><span style='margin-left:10px'>"+res.message+"</span></span>"
                messager('成功',tip,4000,'info');

                $('#editDeptWindow').window('close');
                $("#editDeptForm").form("clear");
                $('#deptTree').tree('reload');
                $('#deptDataGrid').datagrid('reload');
            }else{
                var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/no.png'/><span style='margin-left:10px'>"+res.message+"</span></span>"
                messager('失败',tip,4000,'info');
            }

        });
    }
}

/*
    删除部门
*/
function removeDept(){
    var selected = $('#deptTree').tree('getSelected');
        if(selected!=null){
            apiDeptManageDelDept(selected.id,function(res){
                if(res.status==200){
                    var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/ok.png'/><span style='margin-left:10px'>"+res.message+"</span></span>"
                    messager('成功',tip,4000,'info');
                    $('#deptTree').tree('reload');
                    $('#deptDataGrid').datagrid('reload');
                }else{
                     var tip="<span style='height=20px;line-height:20px;'><img src='js/jquery-easyui-1.7.0/themes/icons/no.png'/><span style='margin-left:10px'>"+res.message+"</span></span>"
                    messager('失败',tip,4000,'info');
                    $('#deptTree').tree('reload');
                    $('#deptDataGrid').datagrid('reload');
                }
            });
        }
}

/*
    点击查询获取列表
*/
function searchDeptList(){
    var evpName=$('#deptNameCBBox').combobox('getValue');
    var evpCode=$('#evpCodeCBBox').combobox('getValue');
    $('#deptDataGrid').datagrid('load',{
        evpName: evpName,
        evpCode: evpCode
    });
}


function clearForm(){
    $("#editDeptForm").form('clear');
}